package com.fenghongzhang.day019;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * ${FENG}
 * 2019-07-24
 */
public class MyAdapter extends BaseAdapter {
    private Context context;
    private List<Song> list;

    public MyAdapter(MainActivity mainActivity, List<Song> list) {
        this.context = mainActivity;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            // 引入布局
            view = View.inflate(context, R.layout.lv_layout, null);
            // 实例化对象
            holder.album = view.findViewById(R.id.lv_album);
            holder.artist = view.findViewById(R.id.lv_artist);
            holder.title = view.findViewById(R.id.lv_title);
            holder.duration = view.findViewById(R.id.lv_duration);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        // 给控件赋值
        Song song = list.get(i);

        //TODO 图片在说
//        holder.album
        holder.title.setText(song.getTitle());
        holder.artist.setText(song.getArtist());
        holder.duration.setText(song.getDuration());
        return view;
    }

    //布局里面有啥, vh就有啥
    class ViewHolder {
        ImageView album; //封面
        TextView title;// 歌曲名
        TextView artist;// 歌手
        TextView duration;// 时长
    }

}
